# coding: UTF-8
import matplotlib.pyplot
#【自定义模块】
import UDM_file
import UDM_data
import UDM_plot


def test() -> None:
    """【函 数】技术验证阶段绘制图像
    """
    # 数据文件的位置
    VAR_data_file = "C:\\Github\\Learn-Matplotlib\\spectrum\\data\\origin_data_2.txt"
    # 读取数据
    data = file.read_data(VAR_data_file)
    # 绘图
    plot.plot_it(data[0], data[1], "blue")
    plot.set_label("Wavelength (nm)", "Intensity (a.u.)")
    # 保存数据
    file.save_image("spectrum\\images\\test.png")
    # 显示（可以不显示，直接保存图片即可）
    plot.display()
    return None


def single_plot(FP_path, display=False):
    """【函 数】绘制单张图像
    """

    # 读取数据
    data = UDM_file.read_data(FP_path)
    # 截取数据
    (a, b) = UDM_data.split_by_value(data)
    # 绘图
    UDM_plot.plot_it(data[0][a:b], data[1][a:b])
    UDM_plot.set_label("Wavelength (nm)", "Intensity (a.u.)")
    # 显示（默认不显示）
    if (display == True):
        UDM_plot.display()
    elif (display == False):
        # 在保存图片之前关闭窗口会导致保存的图片为空白
        pass
    return None


def batch_plot_spectrum() -> None:
    """【函 数】绘制多张图像
    """
    STR_data_folder = r"C:\\Github\\Learn-Matplotlib\\spectrum\\data\\"
    STR_image_folder = r"C:\\Github\\Learn-Matplotlib\\spectrum\\images\\"
    LIST_file_list = UDM_file.get_filename(STR_data_folder)
    for STR_file_name in LIST_file_list:
        STR_image_name = STR_file_name.split(".")[-2] + ".png"
        STR_data_path = STR_data_folder + STR_file_name
        STR_image_path = STR_image_folder + STR_image_name
        print("绘制数据：", STR_data_path)
        single_plot(STR_data_path)
        UDM_file.save_image(STR_image_path)
        print("保存图片：", STR_image_path)
        matplotlib.pyplot.close("all")  # 虽然不显示绘图窗口，但实际上窗口仍然被创建了，所以需要关闭窗口
    return None


def plot_by_time() -> None:
    """【函 数】按时间顺序绘制图像
    """
    STR_data_folder = r"C:\\Github\\Learn-Matplotlib\\spectrum\\data\\"
    LIST_file_list = UDM_file.get_filename(STR_data_folder)

    x_axis = []
    y_axis = []
    intensity_list = []
    filename_list = []
    intensity_dict = {}

    for STR_file_name in LIST_file_list:
        STR_data_path = STR_data_folder + STR_file_name
        # 读取数据
        data = UDM_file.read_data(STR_data_path)
        # 截取数据（寻找最大值）
        max_intensity = max(data[1])
        intensity_list.append(max_intensity)
        intensity_dict[STR_file_name.split(".")[-2]] = max_intensity
    intensity_dict_sort = sorted(intensity_dict.items(), key=lambda x:x[1], reverse=True)
    
    # print(LIST_file_list)
    # print(intensity_list)
    # print(intensity_dict)
    print(intensity_dict_sort)
    for intensity in intensity_dict_sort:
        x_axis.append(intensity[0])
        y_axis.append(intensity[1])
    
    
    # i = 0
    # while (i < len(x_axis)):
    #     UDM_file.save_txt("output\\max_intensity.txt", str(x_axis[i]) + str(y_axis[i]))
    

    # for k, v in i_dict.item():
    #     print("键：", k)
    #     print("值：", v)

    # x_axis = [5,15,30,45,60,75,90,105,120,135,150,165,180,195,210,225,240,255,270,285,300,315,330,345,360,375,390,420,450,480,510,540,570,600]
    # x_axis = [5,15,30,45,60,75,90,105,120,135,150,165,180,210,240,270,300,360,420,480,540,600]
    print("时间:", x_axis)
    print("强度:", y_axis)
    print("个数:", len(x_axis))
    # 绘图
    UDM_plot.plot_it(x_axis, y_axis)
    UDM_plot.set_label("Time (s)", "Intensity (a.u.)")
    STR_image_path = r"C:\\Github\\Learn-Matplotlib\\spectrum\\images\\time"
    UDM_file.save_image(STR_image_path)
    print("保存图片：", STR_image_path)
    matplotlib.pyplot.close("all")  # 虽然不显示绘图窗口，但实际上窗口仍然被创建了，所以需要关闭窗口
    return None


if (__name__ == "__main__"):
    # test()

    # VAR_data_file = "C:\\Github\\Learn-Matplotlib\\spectrum\\data\\origin_data_2.txt"
    # data = file.read_data(VAR_data_file)
    # print(data[0][2:6])
    
    batch_plot_spectrum()
    # plot_by_time()
    UDM_file.save_txt("output\\max_intensity.txt", "data")
    # 
    
